
<template>
  <div style="padding-left:10px; padding-right: 10px; width: calc(100vw - 200px)">
    <!--    搜索区域-->
    <div style="margin: 10px 0">
      <span style="color: #611ee3;font-size: large;">课程名：</span>
      <el-input v-model="search" placeholder="请输入课程名" style="width: 20%" clearable></el-input>
      <el-button style="margin-left: 5px; background-color: #409EFF; color: white" @click="load" plain>查询</el-button>
    </div>
    <el-table
        :data="tableData"
        border
        stripe
        style="width: 100%">
      <el-table-column
          prop="id"
          label="ID"
          sortable>
      </el-table-column>
      <el-table-column
          prop="courseName"
          label="课程名">
      </el-table-column>
      <el-table-column
          prop="coachName"
          label="教练名">
      </el-table-column>
      <el-table-column
          label="课程封面">
        <template #default="scope">
          <div class="demo-image__preview">
            <el-image
                style="width: 100px; height: 100px"
                :src="scope.row.courseImg"
                :preview-src-list="[scope.row.courseImg]">
            </el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column
          prop="courseInfo"
          label="课程信息">
      </el-table-column>
      <el-table-column
          prop="coursePrice"
          label="单价">
      </el-table-column>
      <!--      <el-table-column-->
      <!--          prop="courseStaus"-->
      <!--          label="发布时间">-->
      <!--      </el-table-column>-->
      <el-table-column
          label="课程状态">
        <template #default="scope">
          <span v-if="scope.row.courseStatus==='1'">未开始</span>
          <span v-if="scope.row.courseStatus==='2'">进行中</span>
          <span v-if="scope.row.courseStatus==='3'">已结束</span>
        </template>
      </el-table-column>
      <el-table-column
          prop="startDate"
          label="开始时间">
      </el-table-column>
      <el-table-column
          prop="endDate"
          label="结束时间">
      </el-table-column>
      <el-table-column
          label="操作">
        <template #default="scope">
          <div style="display: flex; align-content: space-between">
            <el-button @click="status(scope.row)"  v-if="scope.row.orderStatus==='0'" type="primary">预约</el-button>
            <el-button @click="cancelOrder(scope.row)"  v-if="scope.row.orderStatus==='1'" type="primary">取消预约</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
        <div style="margin: 10px 0">
          <el-dialog
              title="提示"
              v-model="deleteVisible"
              width="30%">
            <p style="margin-bottom: 30px;font-size: 17px;color: #f73c3c;">确定预约该课程吗？</p>
            <span slot="footer" class="dialog-footer">
          <el-button @click="this.deleteVisible = false">取 消</el-button>
          <el-button type="primary" @click="orderStatus(this.courseId)">确 定</el-button>
        </span>
          </el-dialog>
          <el-dialog
              title="提示"
              v-model="cancelVisible"
              width="30%">
            <p style="margin-bottom: 30px;font-size: 17px;color: #f73c3c;">确定取消预约该课程吗？</p>
            <span slot="footer" class="dialog-footer">
          <el-button @click="this.cancelVisible = false">取 消</el-button>
          <el-button type="primary" @click="cancelCourse(this.courseId)">确 定</el-button>
        </span>
          </el-dialog>
        </div>
  </div>
</template>

<script>


import request from "@/utils/request";

export default {
  name: 'course',
  components: {

  },
  data() {
    return {
      member: {},
      search:'',
      currentPage4: 1,
      pageSize: 10,
      total:0,
      dialogVisible:false,
      tableData: [],
      user:{},
      deleteVisible: false,
      courseId:'',
      openFlag:true,
      cancelVisible:false,
      orderId:''
    }
  },
  created() {
    let Str =sessionStorage.getItem("member") || '{}';
    this.member = JSON.parse(Str);
    this.load();
    let userStr =sessionStorage.getItem("user") || '{}';
    this.user = JSON.parse(userStr);
//请求后端服务器，确认当前登录用户的合法性
    request.get("/api/user/"+this.user.id).then(res=>{
      if (res.code==='0'){
        this.user = res.data;
      }
    });
  },
  methods:{
    load(){
      request.get("/api/memberOrder/getMemberOrder",{
        params: {
          memberId: this.member.id,
          search:this.search
        }
      }).then(res =>{
        console.log(res);
        this.tableData = res.data;
        // this.total = res.data.total;
      })
    },
    status(row){
      this.courseId=row.courseId;
      this.deleteVisible=true
      this.orderId=row.orderId
    },
    cancelOrder(row){
      this.orderId=row.orderId
      this.courseId=row.courseId;
      this.cancelVisible=true
    },
    cancelCourse(courseId){
      const params= {
        id:this.orderId,
        memberId: this.member.id,
        courseId: courseId,
        orderStatus:'0'
      }
      request.put("/api/memberOrder",params).then(res =>{
        if (res.code === '0') {
          this.$message({
            message: '取消预约成功!',
            type: 'success'
          });
        }else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
        this.cancelVisible=false
        this.load()
      })
    },
    async orderStatus(courseId){
      this.openFlag = true;
      // await request.get("/api/memberOrder/getOrder", {
      //   params: {
      //     memberId: this.member.id,
      //     courseId: courseId,
      //   }
      // }).then(res => {
      //   console.log(res.data)
      //   if (res.data.length > 0) {
      //     this.$message({
      //       message: '该课程已预约!',
      //       type: 'warning'
      //     });
      //     this.openFlag = false;
      //     this.deleteVisible=false
      //   }
      // });
      const params= {
        memberId: this.member.id,
            courseId: courseId,
            orderStatus:'1'
      }
      const params1= {
        id:this.orderId,
        memberId: this.member.id,
        courseId: courseId,
        orderStatus:'1'
      }
      if (this.openFlag){
        if (this.orderId){
          await request.put("/api/memberOrder",params1).then(res =>{
            if (res.code === '0') {
              this.$message({
                message: '预约成功!',
                type: 'success'
              });
            }else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }
            this.deleteVisible=false
            this.load()
          })
        }else{
          await request.post("/api/memberOrder",params).then(res =>{
            if (res.code === '0') {
              this.$message({
                message: '预约成功!',
                type: 'success'
              });
            }else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }
            this.deleteVisible=false
            this.load()
          })
        }

      }

    },
    handleSizeChange(pageSize){//改变当前页面个数
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum){//改变当前页码
      this.currentPage4 = pageNum;
      this.load();
    }
  }
}
</script>
